package com.example.springboottest.student.service.impl;

import com.example.springboottest.base.AbstractService;
import com.example.springboottest.student.service.TempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("tempService")
public class TempServiceImpl extends AbstractService implements TempService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void doCreateTempBySession() {
        String sql = "insert into test.temp_session values('temp_session')";
        jdbcTemplate.execute(sql);
    }

    @Override
    public void doCreateTempByTranscation() {
        String sql = "insert into test.temp_transcation values('temp_transcation')";
        jdbcTemplate.execute(sql);

    }

    @Override
    public void doReadTempBySession() {
        logger.info("doReadTempBySession...");
        String sql = "select * from test.temp_session";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
        rows.stream().forEach(System.out::println);
    }

    @Override
    public void doReadTempByTranscation() {
        logger.info("doReadTempBySession...");
        String sql = "select * from test.temp_transcation";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
        rows.stream().forEach(System.out::println);
    }

    @Override
    public int doGetdOracleSessionId() {
        logger.info("doReadOracleSession...");
        String sql = "select sid from v$mystat where rownum=1";
        int sessionId = jdbcTemplate.queryForObject(sql, int.class, null);
        logger.info("当前session的ID:\t" + sessionId);
        return sessionId;
    }
}
